const requsts = require("/util/requst");
const util = require("/util/util");
Page({
  data: {
    msgboj:{},
    orderId:"",
    userId:"",
		datastr:"",
		issub:false,
		tongyi:"/image/landui.jpg",
		notongyi:"/image/moren.png",
  },
  ditaddress(){
    my.navigateTo({
       url: '/page/common/editaddress/editaddress',
    });
  },
	tongyifn(){
		if(this.data.notongyi=="/image/moren.png"){
			this.setData({
				notongyi:"/image/landui.jpg",
				issub:true,
			})
		}else{
			this.setData({
				notongyi:"/image/moren.png",
				issub:false
			})
		}
	},
  submit(){
    my.navigateTo({
       url: '/page/common/addaddress/addaddress',
    });
  },
  showToastSuccess(){
		var param={
			userId:this.data.userId,
			orderId:this.data.orderId
		}
		var that=this
    my.request({
      url: requsts.url+"/order/confirmOrder",
      method: 'POST',
      headers: {'content-type': 'application/x-www-form-urlencoded'},
      data:param,
      dataType: 'json',
      success: function(res) {
				if(res.data.code!=0){
						util.toastfn(res.data.msg)
						return
				}
				that.setData({
					datastr:res.data.data
				})
				// my.showToast({
				// 	type: 'success',
				// 	content: '购买成功',
				// 	duration: 3000,
				// 	success: () => {
					
				// 	},
				// })
				that.tradePayFn()
      },
      fail: function(res) {
          util.toastfn(res.errorMessage)
      },
    });
    // my.showToast({
    //   type: 'success',
    //   content: '购买成功',
    //   duration: 3000,
    //   success: () => {
       
    //   },
    // });
  },
	tradePayFn(){
			// this.setData({
			// 	issub:false
			// })
			var that=this
			my.tradePay({
				orderStr:that.data.datastr,
				success: (res) => {
					//  my.navigateTo({
          //   url:'/page/common/orderdetails/orderdetails?orderId='+that.data.orderId+"&userId="+that.data.orderId,
          // })
          my.switchTab({
            url:'/page/tabBar/component/index',
          })
				},
				fail: (res) => {
					my.alert({
					content: JSON.stringify(res),
				});
				},
			});
	},
  // 人脸采集
  faceVerifyfn(){
			if(!this.data.issub){
					return
			}
			this.setData({
				issub:false
			})
			 my.showLoading({
				content: '请等待...',
			});
			var that=this
      my.getServerTime({
        success: (res) => {
          // my.alert({
          //   content: res.time,
          // });
          my.ap.faceVerify({
              bizId: res.time, //业务流水号，商户自行生成，需要保证唯一性，不超过64位
              bizType: '1', //业务场景参数，‘1’代表人脸采集，请务必填写
              success: (res) => {
									my.hideLoading();
                  that.showToastSuccess()
              },
              fail: (res) => {
								my.hideLoading();
                  util.toastfn(res.errorMessage)
              }
          });
        },
      });
  },
  onLoad(e){
		//this.tradePayFn()
    var param=e.orderId
    var that=this
    my.getStorage({
      key: 'userId',
      success: function(res) {
        console.log(res)
        that.setData({
          userId:res.data.userId,
          orderId:param
        })
        that.init()
      },
      fail: function(res){
      }
    });
  },
  init(){
    var param={
      userId:this.data.userId,
      orderId:this.data.orderId
    }
    var that=this
      my.request({
        url: requsts.url+"/order/getUserOrderDetail",
        method: 'POST',
        headers: {'content-type': 'application/x-www-form-urlencoded'},
        data:param,
        dataType: 'json',
        success: function(res) {
          console.log(res)
          if(res.data.code!=0){
              util.toastfn(res.data.msg)
              return
          }
          that.setData({
              msgboj:res.data.data
          })
        },
        fail: function(res) {
            util.toastfn(res.errorMessage)
        },
      });
  },
	Rentalservices(){
			my.navigateTo({
				url:'/page/common/rentalservices/rentalservices?userId='+this.data.userId+'&orderId='+this.data.orderId,
			})
	}
});